//
//  KATBottomButtons.m
//  KATFramework
//
//  Created by Kantice on 16/4/15.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATBottomButtons.h"



@interface KATBottomButtons() <KATLabelButtonDelegate>

#pragma -mark 属性

///底部容器
@property(nonatomic,retain) UIView *bottomContent;

///顶部容器
@property(nonatomic,retain) UIView *topContent;

///底部模糊背景
@property(nonatomic,retain) UIVisualEffectView *bottomBlur;

///顶部模糊背景
@property(nonatomic,retain) UIVisualEffectView *topBlur;

///顶部容器分割线数组
@property(nonatomic,retain) KATArray<__kindof UIView *> *topLines;

///标签按钮数组
@property(nonatomic,retain) KATArray<__kindof KATLabelButton *> *labels;




@end



@implementation KATBottomButtons

+ (instancetype)buttonsWithLabels:(KATArray<__kindof NSString *> *)labels
{
    if(labels && labels.length>0)
    {
        if(labels.length>BOTTOM_BUTTONS_COUNT_MAX)//超出范围，截断
        {
            labels=[labels subArrayWithRange:NSMakeRange(0, BOTTOM_BUTTONS_COUNT_MAX)];
        }
        
        KATBottomButtons *buttons=[[[self alloc] init] autorelease];
        
        [buttons initData];
        
        //样式
        float padding=MIN(SCREEN_WIDTH,SCREEN_HEIGHT)*0.05;
        float width=buttons.screenSize.width-2*padding;
        float buttonHeight=padding*2.5;
        UIFont *buttonFont=[UIFont systemFontOfSize:buttonHeight*0.382];
        
        CGSize size;
        
        if(labels.length==1)
        {
            size=CGSizeMake(width, buttonHeight+padding*1);
        }
        else
        {
            size=CGSizeMake(width, buttonHeight*labels.length+padding*2);
        }
        
        buttons.contentSize=size;
        
        buttons.isTapShadowToHide=YES;
        buttons.isTapContentToHide=NO;
        buttons.isBlurStyle=NO;
        buttons.placement=CGPointMake(buttons.screenSize.width*0.5, (buttons.screenSize.height-size.height/2.0)-SCREEN_MARGIN_BOTTOM);
        buttons.bgColor=[UIColor clearColor];
        buttons.showMode=KATPopWindowAnimationTypeMovingDown;
        buttons.hideMode=KATPopWindowAnimationTypeMovingDown;
        buttons.showDuration=0.25;
        buttons.hideDuration=0.25;
        
        [buttons initbuttons];
        
        //按钮
        buttons.labels=[KATArray arrayWithCapacity:labels.length];
                
        for(int i=0;i<labels.length;i++)
        {
            NSString *title=(NSString *)[labels get:i];
            
            UILabel *label=[[[UILabel alloc] init] autorelease];
            label.frame=CGRectMake(padding, 0, width-2*padding, buttonHeight);
            label.backgroundColor=[UIColor clearColor];
            label.font=buttonFont;
            label.text=title;
            label.textAlignment=NSTextAlignmentCenter;
            label.lineBreakMode=NSLineBreakByTruncatingTail;
            label.numberOfLines=1;
            label.tag=i;
            label.userInteractionEnabled=YES;
            
            if(i==0)
            {
                label.textColor=KCOLOR_RED;
            }
            else
            {
                label.textColor=KCOLOR_BLUE;
            }
            
            KATLabelButton *labelButton=[KATLabelButton buttonWithLabel:label andAction:nil];
            labelButton.tag=i;
            labelButton.eventDelegate=buttons;
            
            [buttons.labels put:labelButton];
        }
        
        //底部
        buttons.bottomContent=[[[UIView alloc] init] autorelease];
        buttons.bottomContent.backgroundColor=[UIColor clearColor];
        buttons.bottomContent.layer.cornerRadius=8.0;
        buttons.bottomContent.frame=CGRectMake(0, size.height-buttonHeight-padding*0.5, width, buttonHeight);
        buttons.bottomContent.clipsToBounds=YES;
        [buttons.content addSubview:buttons.bottomContent];
        
        //毛玻璃
        buttons.bottomBlur=[[[UIVisualEffectView alloc] init] autorelease];
        buttons.bottomBlur.effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        buttons.bottomBlur.layer.cornerRadius=buttons.bottomBlur.layer.cornerRadius;
        buttons.bottomBlur.frame=buttons.bottomContent.bounds;
        [buttons.bottomContent addSubview:buttons.bottomBlur];
        
        //文本
        [buttons.bottomContent addSubview:(KATLabelButton *)[buttons.labels firstMember]];
        
        if(labels.length>1)
        {
            //顶部
            buttons.topContent=[[[UIView alloc] init] autorelease];
            buttons.topContent.backgroundColor=[UIColor clearColor];
            buttons.topContent.layer.cornerRadius=buttons.bottomContent.layer.cornerRadius;
            buttons.topContent.frame=CGRectMake(0, padding*0.5, width, buttonHeight*(labels.length-1));
            buttons.topContent.clipsToBounds=YES;
            [buttons.content addSubview:buttons.topContent];
            
            //毛玻璃
            buttons.topBlur=[[[UIVisualEffectView alloc] init] autorelease];
            buttons.topBlur.effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
            buttons.topBlur.layer.cornerRadius=buttons.topBlur.layer.cornerRadius;
            buttons.topBlur.frame=buttons.topContent.bounds;
            [buttons.topContent addSubview:buttons.topBlur];
            
            //线
            buttons.topLines=[KATArray arrayWithCapacity:labels.length-2];
            
            for(int i=2;i<labels.length;i++)
            {
                UIView *line=[[[UIView alloc] init] autorelease];
                line.frame=CGRectMake(0, (i-1)*buttonHeight, buttons.topContent.frame.size.width, 0.5);
                line.backgroundColor=KCOLOR_LINE;
                [buttons.topContent addSubview:line];
                
                [buttons.topLines put:line];
            }
            
            //文本
            for(int i=1;i<buttons.labels.length;i++)
            {
                KATLabelButton *labelButton=[buttons.labels get:i];
                
                labelButton.frame=CGRectMake(labelButton.frame.origin.x, (buttons.labels.length-1-i)*buttonHeight, labelButton.frame.size.width, labelButton.frame.size.height);
                [buttons.topContent addSubview:labelButton];
            }
        }

        return buttons;
    }
    else
    {
        return nil;
    }
}


- (void)initbuttons
{
    [super initWindow];
}


- (void)buttonTap:(int)index
{
    [self hide];
    
    if(_buttonDelegate && [_buttonDelegate respondsToSelector:@selector(buttonTapedWithIndex:andBottomButtons:)])
    {
        [_buttonDelegate buttonTapedWithIndex:index andBottomButtons:self];
    }
}


//按钮点击
- (void)labelButtonTaped:(KATLabelButton *)button
{
    [self buttonTap:(int)button.tag];
}


//调整窗口
- (void)resizeWindow
{
    if(SCREEN_HEIGHT<SCREEN_WIDTH || self.bounds.size.width>SCREEN_HEIGHT)//横屏下隐藏
    {
        [self hide];
    }
}


- (void)dealloc
{
    [_bottomContent release];
    [_topContent release];
    [_bottomBlur release];
    [_topBlur release];
    [_topLines release];
    [_labels release];
    
    [super dealloc];
}



@end
